package com.ailot.cloud.auth.handler;

import com.ailot.cloud.base.common.dto.Result;
import com.ailot.cloud.base.security.utils.ResponseUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ClassName:JwtAuthenticationSuccessHandler
 * Description: 登录失败处理
 */
@Slf4j
@AllArgsConstructor
@Component
public class JwtAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        log.info("登录失败:{}", exception.getLocalizedMessage());
        ResponseUtil.response(response, Result.fail(exception.getLocalizedMessage()));
    }
}
